package com.test.dao.impl;

import com.test.dao.IBuildMeterCurrentDao;
import com.test.model.BuildMeterCurrentData;
import com.test.model.BuildValve;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by ZZF on 2017/7/3.
 */
@Repository
public class BuildMeterCurrentDaoImpl extends BaseDaoImpl<BuildMeterCurrentData> implements IBuildMeterCurrentDao {
    @Override
    public List<Map<String, Object>> findStationBranch() {

        String Sql = "SELECT b.stationBranchId,b.uniqueId,a.* FROM rcc_d_buildmetercurrentdata a " +
                "INNER JOIN rcc_e_buildmeter b on b.uniqueId =a.buildMeterId";

        Query query = getSession().createSQLQuery(Sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

        return query.list();
    }

    @Override
    public BuildMeterCurrentData findBuildMeterCurrent(int unitId, int buildId) {

        StringBuffer sql = new StringBuffer();

        sql.append("SELECT b.* from rcc_e_buildmeter a INNER JOIN rcc_d_buildmetercurrentdata b  ON  b.buildMeterId=a.uniqueId WHERE 1=1 ");

        if (unitId != 0) {
            sql.append("and a.unitId=" + unitId);
        } else {

            sql.append("AND ISNULL(a.unitId) AND buildId=" + buildId);
        }


        Query query = getSession().createSQLQuery(sql.toString()).addEntity(BuildMeterCurrentData.class);


        return (BuildMeterCurrentData) query.uniqueResult();
    }

}
